<?php
/**
 * Created by PhpStorm.
 * User: patrick wu
 * Date: 2020/10/30
 * Time: 11:12
 */
namespace App\Http\Controllers\Backend;

use App\Service\Live\AnchorService;
use App\Http\Model\MongoDB\LiveList;
use App\Service\Live\ClientService;
use App\Http\Controllers\Live\BaseController as LiveBaseController;

class ReplyController extends BaseController
{
    /**
     * stop user talk
     * @param $websocket
     * @param $data
     */
    public function anchorBlock($websocket, $data)
    {
        $room_name  =   $websocket->getUserId();
        $service    =   new AnchorService();
        $service->blockUser($room_name, $data['user_id']);

        //notify the client user
        $websocket->toUserId($data['user_id'])->emit('clientBlock', ['user_id' => $data['user_id']]);

        $client     =   new ClientService();
        $user_info  =   $client->setUser($data['user_id']);

        //notify the whole room user
        $websocket->to($this->current_room)->emit('clientWelcome', ['type' => LiveBaseController::BLOCK_WELCOME, 'msg' => sprintf(LiveBaseController::WELCOME_DESC[LiveBaseController::BLOCK_WELCOME], ($user_info['nick_name'] ?? ''))]);

        //notify users
        $block_info     =   $service->getBlockUser($room_name, true);

        //notify the backend user
        $websocket->to($this->current_room)->toUserId($this->room_id)->emit('backendBlockUsers', $block_info);
    }

    /**
     * open user talk
     * @param $websocket
     * @param $data
     */
    public function anchorUnblock($websocket, $data)
    {
        $room_name  =   $websocket->getUserId();
        $service    =   new AnchorService();
        $service->unblockUser($room_name, $data['user_id']);

        //notify the client user
        $websocket->toUserId($data['user_id'])->emit('clientUnblock', $data);

        $client     =   new ClientService();
        $user_info  =   $client->setUser($data['user_id']);

        //notify the whole room user
        $websocket->to($this->current_room)->emit('clientWelcome', ['type' => LiveBaseController::UNBLOCK_WELCOME, 'msg' => sprintf(LiveBaseController::WELCOME_DESC[LiveBaseController::UNBLOCK_WELCOME], ($user_info['nick_name'] ?? ''))]);

        //notify users
        $block_info     =   $service->getBlockUser($room_name, true);

        //notify the backend user
        $websocket->to($this->current_room)->toUserId($this->room_id)->emit('backendBlockUsers', $block_info);
    }

    /**
     * anchor talk to every one
     * @param $websocket
     * @param $data
     */
    public function anchorSpeak($websocket, $data)
    {
        $send_msg   =   $data['message'] ?? '';
        if (!empty($send_msg)) {
            $reply_info['anchor_id']    =   LiveList::where(['Id' => $websocket->getUserId()])->value('HostId') ?? '';
            $reply_info['message']      =   $send_msg;
            $reply_info['room_id']      =   $websocket->getUserId();
            $websocket->to($this->current_room)->emit('clientReply', $reply_info);
        }
    }

    /**
     * anchor private talk to user
     * @param $websocket
     * @param $data
     */
    public function anchorPrivate($websocket, $data)
    {
        $send_msg   =   $data['message'] ?? '';
        $user_id    =   $data['user_id'] ?? '';
        if (!empty($send_msg)) {
            $reply_info['anchor_id']    =   LiveList::where(['Id' => $websocket->getUserId()])->value('HostId') ?? '';
            $reply_info['message']      =   $send_msg;
            $reply_info['room_id']      =   $websocket->getUserId();
            $reply_info['user_id']      =   $user_id;
            if (!empty($user_id)) {
                //if there is a user, talk to the user
                $websocket->toUserId($user_id)->emit('clientPrivate', $reply_info);
            } else {
                //if not, talk to everyone
                $websocket->to($this->current_room)->emit('clientReply', $reply_info);
            }
        }
    }

    /**
     * normal talk to user
     * @param $websocket
     * @param $data
     */
    public function normalSpeak($websocket, $data)
    {
        $receive['message'] =   $data['message'] ?? '';
        $receive['user_id'] =   createRandomNumber(6);
        $receive['nick_name']=  $data['nick_name'] ?? '';

        //broadcast to everyone include sender
        $websocket->to($this->current_room)->emit('clientReceiveMessage', $receive);
    }

}
